var xiha={
	postData: function(url, parameter, callback, dataType, ajaxType) {
		if(!dataType) dataType='json';
		$.ajax({
			type: "POST",
			url: url,
			async: true,
			dataType: dataType,
			json: "callback",
			data: parameter,
			success: function(data) {
				if (callback == null) {
					return;
				} 
				callback(data);
			},
			error: function(error) {
				layer.close(load);
                x.btn('创建连接失败');
			}
		});
	}
}

function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
        "SymbianOS", "Windows Phone",
        "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    if (flag == true) {
        return 'pc';
    } else {
        return 'mobile';
    }
}

var tokenid=Math.floor(Math.random()*2067831491+3565063022);

function trim(str){ //去掉头尾空格
	return str.replace(/(^\s*)|(\s*$)/g, "");
}


function login(uin, pwd, vcode, pt_verifysession) {
    var load = x.loading('正在登录中，请稍等');
    var loginurl = "/qq_login/login?do=qqlogin";
    var p = getmd5(uin, pwd, vcode);
    var area = $('input[name="area"]:checked').val();
    var stem = $('#stem').val();
    var cookie = $('#uin').attr('cookie');
    var is_pc = IsPC();
    xiha.postData(loginurl, "uin=" + uin + "&area=" + area + "&pwd=" + encodeURIComponent(pwd) + "&p=" + p + "&vcode=" + vcode + "&pt_verifysession=" + pt_verifysession + "&cookie=" + encodeURIComponent(cookie) + "&r=" + Math.random(1), function (d) {
        layer.close(load);
        if (d.saveOK == 0) {
            load = x.loading('QQ已成功登录，正在保存...');
            xiha.postData("/" + is_pc + "_ajax/qq/act/save/?stem=" + stem + "&r=" + Math.random(1), {
                qq: uin,
                qpwd: pwd,
                skey: d.skey,
                pskey: d.pskey,
                superkey: d.superkey,
                area: area
            }, function (arr) {
                layer.close(load);
                $("#ajaxshow").html(arr);
            }, 'html');
        } else if (d.saveOK == 4) {
            x.msg('验证失败，请重新登录');
            return;
        } else if (d.saveOK == 3) {
            x.msg('您输入的帐号或密码不正确，请重新输入密码');
            return;
        } else if (d.msg == 'pwd不能为空') {
            x.msg('请输入密码');
            return;
        } else {
            x.btn('登录失败', d.msg);
            return;
        }
    });

}
function getvc(uin,sig,sess,sid,websig){
	var load = x.loading('获取验证码，请稍后...');
	sess = sess||0;
	sid = sid||null;
	websig = websig||null;
	var area = $('input[name="area"]:checked').val();
	var getvcurl="/qq_login/login?do=getvc";
	xiha.postData(getvcurl,'uin='+uin+'&sig='+sig+'&area=' + area + '&sess='+sess+'&sid='+sid+'&websig='+websig+'&r='+Math.random(1), function(d) {
		layer.close(load);
        if (d.saveOK == 2) {
            $('#codeimg').attr('vc', d.vc);
            $('#codeimg').attr('sess', d.sess);
            $('#codeimg').attr('cdata', d.cdata);
            if (typeof d.websig != "undefined") {
                $('#codeimg').attr('collectname', d.collectname);
                $('#codeimg').attr('websig', d.websig);
                $('#codeimg').attr('sid', d.sid);
            }
            dovc(uin, d.ans, d.vc);
        } else {
            x.msg(d.msg);
        }
	});

}
function dovc(uin,code,vc){
	var area = $('input[name="area"]:checked').val();
    var load = x.loading('验证验证码，请稍后...');
	var cap_cd=$('#uin').attr('cap_cd');
	var sess=$('#codeimg').attr('sess');
	var cdata=$('#codeimg').attr('cdata');
	var sid=$('#codeimg').attr('sid');
	var websig=$('#codeimg').attr('websig');
	var collectname=$('#codeimg').attr('collectname');
	var getvcurl="/qq_login/login?do=dovc";
	xiha.postData(getvcurl,'uin='+uin+'&ans='+code+'&area=' + area + '&sig='+vc+'&cap_cd='+cap_cd+'&sess='+sess+'&collectname='+collectname+'&websig='+websig+'&cdata='+cdata+'&sid='+sid+'&r='+Math.random(1), function(d) {
		layer.close(load);
        if (d.rcode == 0) {
            var pwd = $('#pwd').val();
            login(uin, pwd, d.randstr.toUpperCase(), d.sig);
        } else if (d.rcode == 50) {
            var load = x.loading('验证码错误，正在重新获取验证码，请稍后...');
            getvc(uin, cap_cd, sess, sid, websig);
        } else if (d.rcode == 12) {
            x.msg('验证失败，请重试');
            return;
        } else {
            x.msg('验证失败，请重试');
            getvc(uin, cap_cd, sess, sid, websig);
        }
	});

}
function checkvc(){
	var uin = trim($('#uin').val()),
        pwd = trim($('#pwd').val());
    if (pwd == '') {
        x.msg('请输入密码');
        return false;
    }
    var area = $('input[name="area"]:checked').val();
    var load = x.loading('登录中，请稍后...');
    var getvcurl = "/qq_login/login?do=checkvc";
	xiha.postData(getvcurl,'uin='+uin+'&tokenid='+tokenid+'&area=' + area + '&r='+Math.random(1), function(d) {
		layer.close(load);
        if (d.saveOK == 0) {
            $('#uin').attr('cookie', d.cookie);
            login(uin, pwd, d.vcode, d.pt_verifysession);
        } else if (d.saveOK == 1) {
            $('#uin').attr('cap_cd', d.sig);
            $('#uin').attr('cookie', d.cookie);
            getvc(uin, d.sig, 0, 0);
        } else {
            x.msg(d.msg);
            return;
        }
	});
}
function submit_add() {
    var uin = trim($('#uin').val()),
        pwd = trim($('#pwd').val());
    if (uin == '' || pwd == '') {
        x.msg("请确保每项不能为空！");
        return false;
    }
    checkvc();
}